import {
  getKeys,
  setKeys,
  removeKeys,
  mapToObj,
  objTransformMap
} from './useKeys'

import { StorageConfig } from './types'

import { configSetting } from '@/config'

/**
 * window.localStorage 浏览器永久缓存
 * @method setCath 设置永久缓存
 * @method getCache 获取永久缓存
 * @method deleteCatch 移除永久缓存
 * @method clearCache 移除全部永久缓存
 */
/* 废弃 */

class LocalCache {
  // 添加
  setCath(key: string, value: any) {
    window.localStorage.setItem(key, JSON.stringify(value))
  }
  // 查找
  getCache(key: string) {
    // obj=>sting=>obj
    const value = window.localStorage.getItem(key)
    if (value) {
      return JSON.parse(value)
    }
  }
  // 删除
  deleteCatch(key: string) {
    window.localStorage.removeItem(key)
  }
  // 清理
  clearCache() {
    window.localStorage.clear()
  }
}

/**
 * window.localStorage 浏览器永久缓存
 * @method setStorage 设置永久缓存
 * @method getStorage 获取永久缓存
 * @method removeStorage 移除永久缓存
 * @method clearStorage 移除全部永久缓存
 */

class DefaultStorage {
  setStorage(key = '', value = {}) {
    try {
      window.localStorage.setItem(setKeys(key), JSON.stringify(value))
    } catch (error) {
      console.error(error)
    }
  }
  getStorage(key = '') {
    try {
      const value = window.localStorage.getItem(getKeys(key))
      if (value) return JSON.parse(value || ({} as any))
    } catch (error) {
      console.error(error)
    }
  }
  removeStorage(key = '') {
    try {
      window.localStorage.removeItem(removeKeys(key))
    } catch (error) {
      console.error(error)
    }
  }
  clearStorage() {
    try {
      window.localStorage.clear()
    } catch (err) {
      console.error(err)
    }
  }

  /* 针对 map 存储 */
  setMapStroage(key: string, map: Map<string, boolean>) {
    try {
      const mapObj = mapToObj(map)
      window.localStorage.setItem(setKeys(key), JSON.stringify(mapObj))
    } catch (err) {
      console.error(err)
    }
  }

  getMapStorage(key: string) {
    try {
      const value = window.localStorage.getItem(getKeys(key))
      if (value) return objTransformMap(JSON.parse(value))
    } catch (err) {
      console.error(err)
    }
  }
}

/**
 * 建议使用 该 存储方法
 * 浏览器永久缓存
 * @method set 设置永久缓存
 * @method get 获取永久缓存
 * @method remove 移除永久缓存
 * @method clear 移除全部永久缓存
 */

class MyLocalStorage {
  storage: globalThis.Storage
  config: StorageConfig
  constructor(config: StorageConfig) {
    this.config = config
    this.storage = window[config.type]
  }

  set(key = '', value = {}) {
    try {
      this.storage.setItem(setKeys(key), JSON.stringify(value))
    } catch (error) {
      console.error(error)
    }
  }

  get(key: string) {
    try {
      const value = this.storage.getItem(getKeys(key))
      if (value) return JSON.parse(value || ({} as any))
    } catch (error) {
      console.error(error)
    }
  }

  remove(key: string) {
    try {
      this.storage.removeItem(removeKeys(key))
    } catch (error) {
      console.error(error)
    }
  }

  clear() {
    try {
      this.storage.clear()
    } catch (error) {
      console.error(error)
    }
  }

  setMap(key: string, map: Map<string, boolean>) {
    try {
      const mapObj = mapToObj(map)
      this.storage.setItem(setKeys(key), JSON.stringify(mapObj))
    } catch (err) {
      console.error(err)
    }
  }

  getMap(key: string) {
    try {
      const value = this.storage.getItem(getKeys(key))
      if (value) return objTransformMap(JSON.parse(value))
    } catch (err) {
      console.error(err)
    }
  }
}

const storage = new LocalCache()
const defaultStorage = new DefaultStorage()
const myLocalStorage = new MyLocalStorage({ type: configSetting.storageType })

export { defaultStorage, storage, myLocalStorage as db }
